package com.tencent.qplus.paddata.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

import javax.mail.MessagingException;

public class MailAlarm {


	public static void alarm(Exception e){
		MailPerson[] alarmReceiverList = ConfigUtil.getMailRecipientList("alarm_recipient");
		MailPerson mailPerson = new MailPerson("shiangzhang@tencent.com","张祥");
		try {
			MailUtils.sendHtmlEmail("数据统计报警邮件", getExceptionContent(e), alarmReceiverList, null, mailPerson);
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		} catch (MessagingException e1) {
			e1.printStackTrace();
		}
	}
	
	public static String getExceptionContent(Exception e){
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		StringBuilder sb = new StringBuilder();
		e.printStackTrace(new PrintStream(bout));
		ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
		BufferedReader br = new BufferedReader(new InputStreamReader(bin));
		String line = null;
		try {
			while((line=br.readLine())!=null){
				sb.append(line).append("<br>\r\n");
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return sb.toString();
	}
	
}
